import re

def renumber_headings_with_prefix(md_text: str, start_number_str: str = "1"):
    lines = md_text.splitlines()

    heading_pattern = re.compile(r'^(#{2,6})\s+(.*)')
    old_num_pattern = re.compile(r'^\s*\d+(\.\d+)*\s+')

    prefix = start_number_str.rstrip('.')
    first_title_processed = False
    counter = 0
    result_lines = []

    for line in lines:
        m = heading_pattern.match(line)
        if m:
            hashes = m.group(1)  # 保留#
            title_text = m.group(2).strip()
            title_text = old_num_pattern.sub('', title_text)  # 去旧编号
            if not first_title_processed:
                # 第一个一级标题用完整起始编号，保留#
                result_lines.append(f"{hashes} {prefix} {title_text}")
                first_title_processed = True
            else:
                counter += 1
                # 保留#，编号递增
                result_lines.append(f"{hashes} {prefix}.{counter} {title_text}")
        else:
            # 非标题行保持原样，不处理也不输出编号
            result_lines.append(line)

    return '\n'.join(result_lines)


if __name__ == "__main__":
    # 读入文件内容
    with open('D:\\hbyt\\AI智能投标\\markdown_output.md', 'r', encoding='utf-8') as f:
        md_content = f.read()
    start_num_input = input("请输入起始编号（例如3.2.1）：").strip()
    output = renumber_headings_with_prefix(md_content.strip(), start_num_input)
    print(output)